package cn.buddha.jeres.consumer.controller;

import cn.buddha.jeres.consumer.feign.TongFeign;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ClassName ConsumerController
 * @Author 友野浩二
 * @Description 服务消费者controller测试类
 * @Version 1.0
 */
@RestController
@Slf4j
public class ConsumerController {

    // 通过注解@Autowired使用定义feign的客户端
    @Autowired
    private TongFeign tongFeign;

    @RequestMapping(method = RequestMethod.GET, value = "/client")
    public String consumerHello(@RequestParam(name = "lover") String lover) {
        log.info("ConsumerController.consumerHello starting...");
        // 这里的使用本地Java API的方式调用远程的Restful接口
        String returnMsg = "通过feign调用远程服务tong-eureka-provider的restful接口/loveanswer的结果: ";

        // 请求 接口 /loveanswer
        String tongProvider = null;
        try {
            tongProvider = this.tongFeign.loveAnswer(lover);
        } catch (Exception e) {
            log.error("调用tong-eureka-provider的restful接口/loveanswer异常", e);
        }

        // 如果 tongProvider 为 null,则说明有可能连接超时了
        if (null == tongProvider) {
            tongProvider = "没有得到响应结果";
        }
        return returnMsg.concat(tongProvider);
    }

}
